package com.example.receiver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

public class BlackgroupService extends IntentService {

	public static Boolean isLooping = true;
	public String[] lat, lng, voiceurl;
	String emerlat;
	String emerlng;
	String voice;
	Context context;

	// private volatile Thread thread;

	// String a = "";
	public BlackgroupService() {
		super(BlackgroupService.class.getName());
		// TODO Auto-generated constructor stub
		Log.d("1", "1");
	}

	@SuppressWarnings("static-access")
	@Override
	protected void onHandleIntent(Intent intent) {
		// TODO Auto-generated method stub
		Log.d("start service", "1");

	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Log.d("1", "1");
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		Log.d("1", "2");

		thread.start();

	}

	Thread thread = new Thread() {
		int i = 0;

		@Override
		public void run() {

			try {

				while (true) {

					i++;

					if (BlackgroupService.isLooping) {
						FeadData post = new FeadData();
						post.execute("http://172.16.149.122/myexample/feedData.php");// 192.168.1.100
						BlackgroupService.isLooping = false;

					} else {
						Log.d("xxxxxxxxxxxxx", "else");

					}
					sleep(3000);
				}

			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}
	};

	public class FeadData extends AsyncTask<String, Void, String> {

		String result = "";

		@Override
		protected String doInBackground(String... urls) {
			// TODO Auto-generated method stub
			HttpClient httpclient = new DefaultHttpClient();
			HttpGet httpget = new HttpGet(urls[0]);
			BufferedReader in = null;
			try {
				HttpResponse res = httpclient.execute(httpget);
				in = new BufferedReader(new InputStreamReader(res.getEntity()
						.getContent()));
				StringBuffer sb = new StringBuffer("");
				String line = "";
				String NL = System.getProperty("line.separator");
				while ((line = in.readLine()) != null) {
					sb.append(line + NL);
				}
				in.close();
				result = sb.toString();
				Log.d("result", "" + result);
			} catch (ClientProtocolException e) {
				Toast.makeText(getApplicationContext(), e.getMessage(),
						Toast.LENGTH_LONG).show();
			} catch (IOException e) {
				// TODO: handle exception
				Toast.makeText(getApplicationContext(), e.getMessage(),
						Toast.LENGTH_LONG).show();
			}

			return result;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			if (!result.equals(new String(""))) {
				System.out.println(result);
				parseJSON(result);
			}
		}

	}

	private void parseJSON(String result) {
		double emerlattest = 0;
		double emerlngtest = 0;

		try {
			JSONObject jObject = new JSONObject(result);
			System.out.println("result#1 : " + result);
			JSONArray data = jObject.getJSONArray("data");
			lat = new String[data.length()];
			lng = new String[data.length()];
			voiceurl = new String[data.length()];

			for (int i = 0; i < data.length(); i++) {

				JSONObject c = data.getJSONObject(i);
				emerlat = c.getString("startlat");
				emerlng = c.getString("startlng");
				voice = c.getString("VoiceMsgURL");
				lat[i] = emerlat;
				lng[i] = emerlng;
				voiceurl[i] = voice;

				System.out.println("emerlat : " + emerlat);
				System.out.println("emerlattest : " + emerlattest);
				Intent intentt = new Intent(getApplicationContext(),
						BroadreceiverReceiveData.class);
				intentt.putExtra("emerlat1", emerlat);
				intentt.putExtra("emerlng1", emerlng);
				intentt.putExtra("VoiceMsgURL", voice);
				Log.v("already sent xxxxxxx", "sent broadcast");
				sendBroadcast(intentt);
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}


	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.d("service was destroy", "service was destroy");
	
	}

}
